package client.messages;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JFrame;

import miscellaneous.Storage;
import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import data.Invite;
import data.Message;
import data.SynchronizedList;

public class InviteOverview extends JFrame{
	private JLabel inviteTrack;
	private JButton rejectButton;
	private JButton acceptButton;
	private JButton backButton;
	private JButton nextButton;
	private JButton closeButton;
	
	private static InviteOverview existingOverview;
	
	private SynchronizedList<Message> messages;
	private List<Invite> invites;
	private JPanel cards;
	
	private int numberOfCards = 0;
	private int currentCard = 1;
	public InviteOverview() {
		setTitle("Møteinnkalling(er)");
		
		cards = new JPanel(new CardLayout());
		messages = Storage.messages;
		invites = new ArrayList<Invite>();
		for (int i = 0; i < messages.size(); i++) {
			if(messages.get(i) instanceof Invite && ((Invite)messages.get(i)).getReceiver() == Storage.getloggedInUser() && 
					((Invite)messages.get(i)).getAnswer() == Invite.ANSWER_UNKOWN){
				InviteCard ic = new InviteCard((Invite) messages.get(i));
				cards.add(ic, ""+i);
				numberOfCards++;
				invites.add((Invite) messages.get(i));
			}
		}
		
		getContentPane().setLayout(new MigLayout("wrap 1", "[][][][][][]", "[][][][]"));
		
		getContentPane().add(cards, "span 6");
		
		inviteTrack = new JLabel("(1/"+numberOfCards+")");
		getContentPane().add(inviteTrack, "cell 5 1");
		
		rejectButton = new JButton("Avslå");
			rejectButton.addActionListener(new ButtonActionListener());
		getContentPane().add(rejectButton, "cell 0 2");
		
		acceptButton = new JButton("Godta");
		acceptButton.addActionListener(new ButtonActionListener());
		getContentPane().add(acceptButton, "cell 1 2");
		
		backButton = new JButton("<");
		backButton.setEnabled(false);
		backButton.addActionListener(new ButtonActionListener());
		getContentPane().add(backButton, "cell 4 2");
		
		nextButton = new JButton(">");
		nextButton.addActionListener(new ButtonActionListener());
		getContentPane().add(nextButton, "cell 5 2");
		
		closeButton = new JButton("Lukk");
		closeButton.addActionListener(new ButtonActionListener());
		getContentPane().add(closeButton, "cell 5 3");
		
		if(currentCard == 1 && numberOfCards == 1) nextButton.setEnabled(false);
		
		pack();
	}

	public static void showOverview(){
		if(existingOverview == null) existingOverview = new InviteOverview();
		existingOverview.setVisible(true);
	}
	public class ButtonActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			CardLayout cl = (CardLayout)(cards.getLayout());
			backButton.setEnabled(true);
			nextButton.setEnabled(true);
			if(e.getSource() == rejectButton){
				InviteCard ic = (InviteCard) cards.getComponent(currentCard-1);
				Invite i = ic.getInvite();
				i.reject();
				i.setReadTime(new GregorianCalendar());
				
				if(currentCard == 1 && numberOfCards == 1){
					currentCard--;
					numberOfCards--;
					cards.remove(currentCard);
					dispose();
				}
				
				if((currentCard != numberOfCards)){
					inviteTrack.setText("("+currentCard+"/"+(--numberOfCards)+")");
					cards.remove(currentCard-1);
				}else if(currentCard == numberOfCards && currentCard != 0 && numberOfCards != 0) {
					inviteTrack.setText("("+--currentCard+"/"+(--numberOfCards)+")");
					cards.remove(currentCard);
					cl.previous(cards);
				}
				if(currentCard == 1 && numberOfCards == 1) dispose();
			}else if(e.getSource() == acceptButton){
				InviteCard ic = (InviteCard) cards.getComponent(currentCard-1);
				Invite i = ic.getInvite();
				i.accept();
				i.setReadTime(new GregorianCalendar());
				
				if(currentCard == 1 && numberOfCards == 1){
					currentCard--;
					numberOfCards--;
					cards.remove(currentCard);
					dispose();
				}

				if((currentCard != numberOfCards)){
					inviteTrack.setText("("+currentCard+"/"+(--numberOfCards)+")");
					cards.remove(currentCard-1);
				}else if(currentCard == numberOfCards && currentCard != 0 && numberOfCards != 0){
					inviteTrack.setText("("+--currentCard+"/"+(--numberOfCards)+")");
					cards.remove(currentCard);
					cl.previous(cards);
				}
			}else if(e.getSource() == backButton){
				cl.previous(cards);
				currentCard--;
				inviteTrack.setText("("+currentCard+"/"+numberOfCards+")");
			}else if(e.getSource() == nextButton){
					nextButton.setEnabled(true);
					cl.next(cards);
					currentCard++;
					inviteTrack.setText("("+currentCard+"/"+numberOfCards+")");
			}else if(e.getSource() == closeButton){
				dispose();
			}
			if(currentCard == 1) backButton.setEnabled(false);
			if(currentCard == numberOfCards) nextButton.setEnabled(false);
		}
		
	}
}
